package com.stedu.mall.orderservice.controller;

import com.stedu.mall.common.bean.Cart;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.CartService;
import com.stedu.mall.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {

 @Autowired
 private CartService cartService;

    @PostMapping("/{goodsId}")
    public RespBean insert(@PathVariable("goodsId") Integer goodsId, @RequestHeader("token") String token)  {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId =(Integer)map.get("id");
        Cart cart = new Cart();
        cart.setGoodsId(goodsId);
        cart.setUserId(userId);
        //添加或修改数量
       cartService.insertOrUpdate(cart);

       return  RespBean.ok("添加购物车成功");
    }

    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id, @RequestHeader("token") String token) throws SteduException {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId =(Integer)map.get("id");
        cartService.delete(id, userId);
        return RespBean.ok("删除成功");
    }

    @PutMapping
    public RespBean update(@RequestBody Cart cart, @RequestHeader("token") String token) throws SteduException {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId =(Integer)map.get("id");
        cartService.update(cart, userId);
        return RespBean.ok("修改成功");
    }
    @GetMapping
    public  RespBean selectCartList(@RequestHeader("token")String token)
    {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId =(Integer)map.get("id");
        Cart condition = new Cart();
        condition.setUserId(userId);
        List<Cart> cartList= cartService.search(condition);
        return RespBean.ok("",cartList);

    }
    @GetMapping("/{id}")
    public  RespBean selectById(@PathVariable("id") Integer id){
        Cart cart = cartService.selectById(id);
        return  RespBean.ok("",cart);
    }
    @DeleteMapping
    public RespBean deleteByIds(@RequestBody Map<String, ArrayList<Integer>>data, @RequestHeader("token") String token ) throws SteduException {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId =(Integer)map.get("id");
       ArrayList<Integer> ids= data.get("ids");
        //删除
        cartService.deleteByIds(ids, userId);
        return  RespBean.ok("删除成功");
    }

    @GetMapping("/ids")
    public  RespBean selectByIds(Integer[] cartIds, @RequestHeader("token") String token) throws SteduException {
        Map<String,Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId =(Integer)map.get("id");
        List<Cart> cartList=cartService.selectByIds(cartIds, userId);

        return  RespBean.ok("查询成功" ,cartList);
    }

}






